package com.tie.jun.ge.xxljob.controller;

import cn.hutool.core.util.RandomUtil;
import com.tie.jun.ge.xxljob.app.TaskManagerApp;
import com.tie.jun.ge.xxljob.domain.TaskManagerDO;
import com.tiejun.ge.common.core.domain.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: springboot
 * @description: 任务管理控制器
 * @author: getiejun
 * @create: 2025-10-27 23:14
 **/
@RestController
@RequestMapping("task")
public class TaskManagerController {

    @Resource
    private TaskManagerApp taskManagerApp;

    @PostMapping("/add")
    public R<?> add() {
        taskManagerApp.add(prepareAddJobParam());
        return R.success();
    }

    @PostMapping("/list")
    public R<?> list() {
        return R.success(taskManagerApp.listAll());
    }

    @PostMapping("/trigger")
    public R<?> trigger() {
        TaskManagerDO taskManagerDO = new TaskManagerDO();
        taskManagerDO.setJobId("7");
        taskManagerApp.trigger(taskManagerDO);
        return R.success();
    }


    private TaskManagerDO prepareAddJobParam() {
        String randomNumbers = RandomUtil.randomNumbers(3);
        TaskManagerDO taskManagerDO = new TaskManagerDO();
        taskManagerDO.setTaskName("Job" + randomNumbers);
        taskManagerDO.setTaskDesc("测试" + randomNumbers);
        taskManagerDO.setScheduleType("CRON");
        taskManagerDO.setScheduleConf("0/2 * * * * ?");
        taskManagerDO.setExecutorHandler("demoJobHandler");
        taskManagerDO.setTriggerStatus("0");
        taskManagerDO.setDelFlag("0");
        return taskManagerDO;
    }
}
